import { _decorator, Component } from 'cc';
const { ccclass, property } = _decorator;
import * as fgui from "fairygui-cc";
import CountdownTimer from '../utils/CountdownTimer';

export default class ToastCtrl {

    private _node: fgui.GComponent;
    private _display: fgui.Controller;
    private _controller: fgui.Controller;
    private _countDownTimer: CountdownTimer;
    private _roundWaittingLabel: fgui.GComponent;
    private _roundReadyLabel: fgui.GComponent;

    constructor(node: fgui.GComponent, display: fgui.Controller) {
        this._node = node;
        this._display = display;
        this._controller = this._node.getController('c1');

        this._roundWaittingLabel = this._node.getChild('n1');
        this._roundReadyLabel = this._node.getChild('n4');
    }

    public showToast(vaule: number, delay: number) {
        this._display.setSelectedIndex(1)
        this._controller.setSelectedIndex(vaule)
        setTimeout(() => {
            this._display.setSelectedIndex(0)
        }, delay * 1000);
    }

    public showRoundWaitingToast() {
        this.showCountdonwToast('waiting');
    }


    public showRoundReadyToast() {
        this.showCountdonwToast('ready');
    }

    showCountdonwToast(type: string) {
        let index = 0;
        let label: fgui.GComponent;
        if (type === 'waiting') {
            index = 0
            label = this._roundWaittingLabel;
        }
        else if (type === 'ready') {
            index = 1;
            label = this._roundReadyLabel;
        }
        if (label == null) return;

        if (this._countDownTimer) {
            this.hideToast()
        }

        this._display.setSelectedIndex(1)
        this._controller.setSelectedIndex(index)
        this._countDownTimer = new CountdownTimer(30, (remain, status) => {
            if (status === 'running') {
                label.text = `(${remain})`
            }
        })
    }


    public hideToast() {
        if (this._countDownTimer) {
            this._countDownTimer.stop();
        }
        this._display.setSelectedIndex(0)
    }
}